/**
 * 
 */
package com.wubo.sec.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wubo.common.util.Assert;
import com.wubo.common.util.ObjectUtils;
import com.wubo.sec.SecurityManager;
import com.wubo.sec.config.LangUtil;
import com.wubo.sec.event.AuthorityEvent;
import com.wubo.sec.event.EntityEvent;
import com.wubo.sec.log.LogHelper;
import com.wubo.sec.model.Authority;
import com.wubo.sec.service.IAuthorityService;
import com.wubo.sec.util.ObjectDifferenceChecker;
import com.wubo.spring.hibernate.BaseHibernateDao;

/**
 * @author wubo
 * @CreateDate 2010-8-3
 * @version 1.0.01
 */
@Service
@SuppressWarnings("unchecked")
public class AuthorityService implements IAuthorityService {
	@Autowired
	private BaseHibernateDao dao;
	
	public BaseHibernateDao getDao() {
		return dao;
	}
	
	public void setDao(BaseHibernateDao dao) {
		this.dao = dao;
	}
	/* (non-Javadoc)
	 * @see com.asc.sf.service.IAuthorityService#create(com.asc.sf.entity.Authority)
	 */
	public String create(Authority auth) {
		Assert.notNull(auth);
		Assert.notNull(auth.getCode());

		java.util.Date now = new java.util.Date();
		String username = SecurityManager.getCurrentUser().getUsername();
		auth.setCreateTime(now);
		auth.setCreateUser(username);
		auth.setUpdateTime(now);
		auth.setUpdateUser(username);
		
		onAdd(auth);
		
		String code = (String) dao.save(auth);
		
		afterAdd(auth);
		
		return code;
	}

	/* (non-Javadoc)
	 * @see com.asc.sf.service.IAuthorityService#delete(com.asc.sf.entity.Authority)
	 */
	public void delete(Authority auth) {
		Assert.notNull(auth);
		
		onDelete(auth);
		
		dao.delete(auth);
		
		afterDelete(auth);
	}

	/* (non-Javadoc)
	 * @see com.asc.sf.service.IAuthorityService#findAll()
	 */
	public List<Authority> findAll() {
		dao.setCacheQueries(true);
		return dao.find("from Authority a order by a.type, a.code");
	}
	
	public List<Authority> find(String filter, Object[] params) {
		return dao.find("select a from Authority a "+filter, params);
	}
	
	public List<Authority> findRunAs(String filter, Object[] params) {
		return find(filter, params);
	}

	/* (non-Javadoc)
	 * @see com.asc.sf.service.IAuthorityService#getSplitPage(int, int)
	 */
	public List<Authority> getSplitPage(int start, int limit, String where, Object... params) {
		return dao.queryForPage(start, limit, "select a from Authority a "+where, params);
	}
	
	public long getTotal(String where, Object... params) {
		Long result = dao.queryForObject("select count(a.code) from Authority a "+where, params);
		return result;
	}

	/* (non-Javadoc)
	 * @see com.asc.sf.service.IAuthorityService#update(com.asc.sf.entity.Authority)
	 */
	public Authority update(Authority auth) {
		Assert.notNull(auth);
		
		Authority temp = get(auth.getCode());
		
		Authority old = temp.clone();
		
		temp.setName(auth.getName());
		temp.setDesn(auth.getDesn());
		temp.setType(auth.getType());
		temp.setLocked(auth.isLocked());
		
		java.util.Date now = new java.util.Date();
		String username = SecurityManager.getCurrentUser().getUsername();
		temp.setUpdateTime(now);
		temp.setUpdateUser(username);
		
		onUpdate(temp);
		
		dao.update(temp);
		
		afterUpdate(old, temp);
		
		return temp;
	}

	public Authority get(String code) {
		return dao.get(Authority.class, code);
	}

	public List<String[]> listName(String name) {
		return dao.find("select a.code,a.name from Authority a where a.code like '%"+name+"%' or a.name like '%"+name+"%'");
	}
	//根据类别查询
	public List<String[]> listType(String type) {
		return dao.find("select a.type from Authority a where a.type like '%"+type+"%'");
	}
	
	/*----- 发布事件  -----*/
	private void onAdd(Authority auth){
		SecurityManager.publishEvent(new AuthorityEvent(auth, EntityEvent.ADD));
	}
	private void onUpdate(Authority auth){
		SecurityManager.publishEvent(new AuthorityEvent(auth, EntityEvent.UPDATE));
	}
	private void onDelete(Authority auth){
		SecurityManager.publishEvent(new AuthorityEvent(auth, EntityEvent.DELETE));
	}
	/*----- 记录日志 -----*/
	protected void afterAdd(Authority auth){
		LogHelper.actionLog(LangUtil.get("CreateAuth"), 
				LangUtil.get("AuthDetail", ObjectUtils.objToString(auth)));
	}
	protected void afterUpdate(Authority old, Authority auth){
		LogHelper.actionLog(LangUtil.get("UpdateAuth"), 
				LangUtil.get("UpdateAuthDetail", ObjectDifferenceChecker.check(old, auth)));
	}
	protected void afterDelete(Authority auth){
		LogHelper.actionLog(LangUtil.get("DeleteAuth"), 
				LangUtil.get("AuthDetail", ObjectUtils.objToString(auth)));
	}
}
